{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# Numpy 学习\n",
    "\n",
    "## 介绍\n",
    "\n",
    "Numpy 是一个强大的 Python 库，用于进行高性能的科学计算和数据分析。\n",
    "\n",
    "## 基本使用\n",
    "\n",
    "导入 Numpy 模块"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "9544d04230c8a424"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "import numpy as np"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "35b83e09a04e8f65"
  },
  {
   "cell_type": "markdown",
   "source": [
    "Numpy 提供多种方式来创建数组，最简单的方式是使用 `array()` 函数，该函数接收 Python 列表和元组作为参数来创建数组。"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "f0fc6fbbab76619f"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "list = [1, 2, 3, 4]\n",
    "tuple = (3.3, 4.4, 5.5, 6.6)\n",
    "\n",
    "np_array1 = np.array(list)\n",
    "np_array2 = np.array(tuple)\n",
    "\n",
    "print(np_array1)\n",
    "print(np_array2)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "89811ff9f2a742da"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "arr = np.array([(1, 3, 5, 7), (2, 4, 6, 8)])\n",
    "\n",
    "arr"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "ed4c827966484d40"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "arr = np.array((1, 2, 3, 4), dtype=float)\n",
    "\n",
    "arr"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "ae70eb054d77130f"
  },
  {
   "cell_type": "markdown",
   "source": [
    "使用 `np.zeros()` 创建由 0 组成的数组，这适合元素个数确定，但是元素内容未知的场景。"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "b459a456fac181fc"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "arr1 = np.zeros(9)  # 一维数组\n",
    "arr2 = np.zeros((3, 4))  # 二维数组\n",
    "\n",
    "print(arr1)\n",
    "print(arr2)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "f168694912aa2198"
  },
  {
   "cell_type": "markdown",
   "source": [
    "使用 `np.ones()` 创建由 1 组成的数组。"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "805fd3db3b3b5fc0"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "arr1 = np.ones(9)\n",
    "arr2 = np.ones((3, 4))\n",
    "\n",
    "print(arr1)\n",
    "print(arr2)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "d07fdfe6248f04a"
  },
  {
   "cell_type": "markdown",
   "source": [
    "使用 np.arange() 创建数列。"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "4863e0915f6fee30"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "arr1 = np.arange(10)\n",
    "arr2 = np.arange(0, 9, 3)\n",
    "\n",
    "print(arr1)\n",
    "print(arr2)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "87ecd6c0d3f0db56"
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 基本运算"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "f5311299613662ca"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "x = np.array([1, 3, 5, 7])\n",
    "y = np.array([2, 4, 6, 8])\n",
    "\n",
    "# 加法\n",
    "result = x + y\n",
    "print(f'x + y = {result}')\n",
    "\n",
    "# 减法\n",
    "result = x - y\n",
    "print(f'x - y = {result}')\n",
    "\n",
    "# 乘法\n",
    "result = x * y\n",
    "print(f'x * y = {result}')\n",
    "\n",
    "# 除法\n",
    "result = x  y\n",
    "print(f'x - y = {result}')"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "d26fad0fbb170dae"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "4186d04979e93ef6"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
